package com.stack.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stack.model.wemedia.pojos.NewsCrawler;
import com.stack.wemedia.mapper.NewsCrawlerMapper;
import com.stack.wemedia.service.NewsCrawlerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 112
 * @description 针对表【wm_news_crawler】的数据库操作Service实现
 * @createDate 2023-11-30 11:30:20
 */
@Service
public class NewsCrawlerServiceImpl extends ServiceImpl<NewsCrawlerMapper, NewsCrawler> implements NewsCrawlerService {

    @Resource
    private NewsCrawlerMapper newsCrawlerMapper;


    @Override
    public Integer insert(NewsCrawler newsCrawler) {
        int i = newsCrawlerMapper.insert(newsCrawler);
        return i;
    }

    @Override
    public List<String> getUrlByChannel(Integer channel) {
        LambdaQueryWrapper<NewsCrawler> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(NewsCrawler::getChannel, channel);
        wrapper.eq(NewsCrawler::getStatus, 0);
        List<NewsCrawler> newsCrawlers = newsCrawlerMapper.selectList(wrapper);
//        newsCrawlers.stream().filter( newsCrawler -> newsCrawler.getStatus() == null).collect(Collectors.toList());
        return newsCrawlers.stream().map(NewsCrawler::getUrl).collect(Collectors.toList());
    }

    @Override
    public void update(NewsCrawler newsCrawler) {
        LambdaUpdateWrapper<NewsCrawler> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(ObjectUtils.isNotEmpty(newsCrawler.getUrl()), NewsCrawler::getUrl, newsCrawler.getUrl());
        wrapper.set(ObjectUtils.isNotEmpty(newsCrawler.getStatus()), NewsCrawler::getStatus, newsCrawler.getStatus());
        newsCrawlerMapper.update(newsCrawler, wrapper);
    }

    @Override
    public List<NewsCrawler> getNews(Integer channel) {
        LambdaQueryWrapper<NewsCrawler> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(NewsCrawler::getChannel, channel);
        wrapper.eq(NewsCrawler::getStatus, 0);
        List<NewsCrawler> newsCrawlers = newsCrawlerMapper.selectList(wrapper);
        return newsCrawlers;
    }


}




